<?php
class a_indexService extends Service{
	protected $url_service;
	public function __construct(){
		parent::__construct();
        
        $this->url_service=load_service('url',P_SVC);
        $this->url_service->set_module_id(11);
        
		$this->table_cate=table('category');
		$this->table=table('arc');
		$this->table_data=table('arc_data');
		$this->table_attrtag=table('attrtaglist');
		$this->table_index_1=table('index_1');
		
		$this->table_wordtag=table('wordtaglist');
		$this->table_index_2=table('index_2');
		
		$this->table_flink=table('flink');
		
		$this->table_hot_tag=table('hot_tag');
	}
	
	//取12个文章级别
	public function menu_levels(){
		$d=array();
		//'static_rule_list_index'=>array('/html/{level_id}/index.html',ROOT.'html/{level_id}/index.html'),
        
		$query="select `id`,`name` from `{$this->table_cate}` order by `mysort` asc";
		$result=$this->m->query($query);
		while(false!==$row=$this->m->fetch_assoc($result)){
            list($row['linkurl'],)=$this->url_service->var_list_index($row['id'])->list_index();
			$d[]=$row;
		}
		return $d;
	}
	
	//文章属性标签 24条 最新的
	public function index_attrtags($num=24){
		$query="select `id`,`tag` from `{$this->table_index_1}` order by `id` desc limit $num";
		return $this->m->mselect($query);
	}
	//热门栏目
	public function index_hot_tag($num=24){
		$query="select `id`,`tag` from `{$this->table_hot_tag}` order by `mysort` asc limit $num";
		return $this->m->mselect($query);
	}
	public function get_tagid($tag){
		$query="select `id` from `{$this->table_index_1}` where `tag`='{$tag}' limit 1";
		return $this->m->get_field($query);
	}
	//头条文章
	public function index_head_article(){
		$query="select `id`,`title`,`htmlurl`,`thumb`,`add_time`,`description` from `{$this->table}` where `status`='1' and `flag`='1' order by `add_time` desc limit 1";
		return $this->m->select($query);
	}
	//头条文章2
	public function index_head_article2($num=4){
		$query="select `id`,`title`,`htmlurl` from `{$this->table}` where `status`='1' and `flag`='1' order by `add_time` desc limit 1,$num";
		return $this->m->mselect($query);
	}
	//幻灯文章
	public function index_pic_article($num=6){
		$query="select `id`,`title`,`htmlurl`,`thumb`,`add_time` from `{$this->table}` where `status`='1' and `flag`='3' order by `add_time` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//各级别推荐文章
	public function index_cmd_article($l,$num=10){
		$query="select `id`,`title`,`htmlurl`,`add_time` from `{$this->table}` where `status`='1' and `flag`='2' and `level_id`='{$l}' order by `add_time` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//推荐文章按数目
	public function index_cmd_article_bynum($num=10){
		$query="select `id`,`title`,`htmlurl`,`add_time` from `{$this->table}` where `status`='1' and `flag`='2' order by `add_time` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//标签名调用文章
	public function index_article_bytag($tag,$num=10){
		$tag=addslashes($tag);
		$query="select `a`.`id`,`a`.`level_id`,`a`.`title`,`a`.`add_time`,`a`.`hits`,`a`.`htmlurl` from `{$this->table_attrtag}` `l` left join `{$this->table}` `a` on `l`.`arc_id`=`a`.`id` where 1 and `a`.`status`='1' and `l`.`tag`='{$tag}' order by `a`.`id` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//标签id调用文章
	public function index_article_bytagid($tagid,$num=10){
		$query="select `tag` from `{$this->table_index_1}` where `id`='{$tagid}' limit 1";
		$tag=$this->m->get_field($query);
		if(!$tag) return false;
		$query="select `a`.`id`,`a`.`level_id`,`a`.`title`,`a`.`add_time`,`a`.`hits`,`a`.`htmlurl` from `{$this->table_attrtag}` `l` left join `{$this->table}` `a` on `l`.`arc_id`=`a`.`id` where 1 and `a`.`status`='1' and `l`.`tag`='{$tag}' order by `a`.`id` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//各级别列表页
	public function index_new_more_link($l){
		$query="select `id`,`name` from `{$this->table_cate}` where `id`='{$l}' limit 1";
		$row=$this->m->select($query);
        list($row['linkurl'],)=$this->url_service->var_list_index($row['id'])->list_index();
		return $row;
	}
	//各级别最新文章
	public function index_new_article($l,$num=10){
		$query="select `id`,`title`,`htmlurl`,`add_time` from `{$this->table}` where `status`='1' and `level_id`='{$l}' order by `add_time` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//最新文章
	public function index_new_article2($num=10){
		$query="select `id`,`title`,`htmlurl`,`add_time` from `{$this->table}` where `status`='1' order by `add_time` desc limit {$num}";
		return $this->m->mselect($query);
	}
	//友链
	public function flink_list($n=''){
		if($n>0) $extra=" limit $n ";
		$query="select `name`,`url` from `{$this->table_flink}` order by `mysort` asc {$extra}";
		return $this->m->mselect($query);
	}
//
}